/**
 * 您的公司.com
 * Copyright (c) 2018-2018 All Rights Reserved.
 */
package com.mei.demo.reflect;

import java.lang.reflect.Field;

/**
 * 反射赋值 Field 
 *
 * @author mei.xiaoliang@qq.com
 */
public class DemoField {

    public static void main(String[] args) {

        try {
            // 对象实例化
            Class<?> dogClass = Class.forName("com.mei.demo.reflect.RefelctDog");
            RefelctDog dog = (RefelctDog) dogClass.newInstance();

            // 设值, private 属性注意打开封装
            Field fieldName = dogClass.getDeclaredField("name");
            fieldName.setAccessible(true);
            fieldName.set(dog, "哈巴狗");

            // 取值
            String dogName = (String) fieldName.get(dog);
            System.out.println(dogName);

            // 获取 Field 类型 
            String string1 = fieldName.getType().getName();
            System.out.println(string1);
            String string2 = fieldName.getType().getSimpleName();
            System.out.println(string2);

        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchFieldException | SecurityException e) {
            e.printStackTrace();
        }

    }

}